package org.x.legalcasemod.service.Impl;




import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.commonmod.enums.LegalStatus;
import org.x.legalcasemod.dto.AllocationLawyerDTO;
import org.x.legalcasemod.dto.AssignLawyerDTO;
import org.x.legalcasemod.dto.GetCaseListDTO;
import org.x.legalcasemod.dto.OperatorCseDTO;
import org.x.legalcasemod.entity.CaseLawyerEntity;
import org.x.legalcasemod.entity.CaseMaterialEntity;
import org.x.legalcasemod.entity.LegalCaseEntity;
import org.x.legalcasemod.mapper.CaseLawyerMapper;
import org.x.legalcasemod.mapper.CaseManagementMapper;
import org.x.legalcasemod.mapper.CaseMaterialMapper;
import org.x.legalcasemod.service.ICaseManagementService;
import org.x.legalcasemod.vo.AcceptCaseVO;
import org.x.legalcasemod.vo.GetCaseDetailVO;
import org.x.legalcasemod.vo.GetCaseListVO;


import java.util.Date;
import java.util.List;

/**
 * 案件表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class CaseManagementServiceImpl extends ServiceImpl<CaseManagementMapper, LegalCaseEntity> implements ICaseManagementService {
    @Autowired
    private CaseManagementMapper legalCaseMapper;

    @Autowired
    private CaseLawyerMapper caseLawyerMapper;

    @Autowired
    private CaseMaterialMapper caseMaterialMapper;
    /**
     * 获取案件列表(分页)
     *
     * @param getCaseListDTO
     * @return
     */
    @Override
    public GetCaseListVO getCaseList(GetCaseListDTO getCaseListDTO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        //构建查询条件
        if (getCaseListDTO.getCaseStatus() != null) {
            queryWrapper.eq("case_status", getCaseListDTO.getCaseStatus());
        }
        if (getCaseListDTO.getType() != null) {
            queryWrapper.eq("type", getCaseListDTO.getType());
        }
        if (getCaseListDTO.getLawyerId() != null) {
            queryWrapper.eq("lawyer_id", getCaseListDTO.getLawyerId());
        }


        Page<LegalCaseEntity> paginate = legalCaseMapper.paginate(getCaseListDTO.getPage(), getCaseListDTO.getPageSize(), queryWrapper);
        List<LegalCaseEntity> legalCaseEntities = paginate.getRecords();
        //组装返回数据
        GetCaseListVO getCaseListVO = new GetCaseListVO();
        getCaseListVO.setLegalCaseList(legalCaseEntities);
        getCaseListVO.setTotal(paginate.getTotalRow());
        return getCaseListVO;
    }

    /**
     * 分配律师
     *
     * @param allocationLawyerDTO
     */
    @Override
    public void allocationLawyer(AllocationLawyerDTO allocationLawyerDTO) {
        LegalCaseEntity caseEntity = UpdateEntity.of(LegalCaseEntity.class);
        //根据案件编号查询案件,这里数据库字段名为case_no,但是前端传递过来的是caseId
        QueryWrapper wrapper = new QueryWrapper().eq("case_no", allocationLawyerDTO.getCaseId());
        caseEntity.setLawyerId(allocationLawyerDTO.getLawyerId());
        caseEntity.setAssignTime(new Date());
        //设置状态为处理中
        caseEntity.setStatus(LegalStatus.ASSIGNED);
        //更新case数据库中的案件表
        legalCaseMapper.updateByQuery(caseEntity, wrapper);
        //更新case数据库中的律师-案件对应表@TODO

    }

    /**
     * 律师接收案件
     * @param caseId
     * @return
     */
    @Override
    public AcceptCaseVO acceptCase(Long caseId) {
        LegalCaseEntity caseEntity = UpdateEntity.of(LegalCaseEntity.class);
        //根据案件编号查询案件,这里数据库字段名为case_no,但是前端传递过来的是caseId
        QueryWrapper wrapper = new QueryWrapper().eq("case_no", caseId);
        //查询案件
        LegalCaseEntity legalCaseEntity = legalCaseMapper.selectOneByQuery(wrapper);
        //负责案件的律师
        Long lawyerId = legalCaseEntity.getLawyerId();
        //当前登录的用户
        Long LoginUserId = StpUtil.getLoginIdAsLong();
        if (lawyerId != LoginUserId) {
            throw new IllegalArgumentException("您没有权限接收这案件");
        }
        caseEntity.setAcceptTime(new Date());
        //设置状态为处理中
        caseEntity.setStatus(LegalStatus.IN_PROGRESS);
        legalCaseMapper.updateByQuery(caseEntity, wrapper);
        //组装VO
        AcceptCaseVO acceptCaseVO = new AcceptCaseVO();
        acceptCaseVO.setCaseId(caseId);
        acceptCaseVO.setAcceptTime(new Date());
        return acceptCaseVO;
    }

    /**
     * 获取案件详情
     * @param caseNo
     * @return
     */
    @Override
    public GetCaseDetailVO getCaseDetail(String caseNo) {
        //根据案件编号查询案件
        LegalCaseEntity legalCaseEntity = legalCaseMapper.selectOneByQuery(new QueryWrapper().eq("case_no", caseNo));
        if (legalCaseEntity == null) {
            throw new IllegalArgumentException("案件不存在");
        }
        //查询案件提交的证据
        List<CaseMaterialEntity> CaseMaterial = caseMaterialMapper.selectListByQuery(new QueryWrapper().eq("case_no", caseNo));
        GetCaseDetailVO getCaseDetailVO = new GetCaseDetailVO();
        getCaseDetailVO.setLegalCase(legalCaseEntity);
        getCaseDetailVO.setCaseMaterialList(CaseMaterial);
        return getCaseDetailVO;
    }

    /**
     * 分配律师
     * @param assignLawyerDTO
     */
    @Override
    public void assignLawyer(AssignLawyerDTO assignLawyerDTO) {
        //更新案件表
        QueryWrapper caseNo = new QueryWrapper().eq("case_no", assignLawyerDTO.getCaseNo());
        LegalCaseEntity legalCaseEntity = UpdateEntity.of(LegalCaseEntity.class);
        legalCaseEntity.setLawyerId(assignLawyerDTO.getLawyerId());
        legalCaseEntity.setAssignTime(new Date());
        //设置状态为处理中
        legalCaseEntity.setStatus(LegalStatus.ASSIGNED);
        //更新case数据库中的案件表
        legalCaseMapper.updateByQuery(legalCaseEntity, caseNo);
        //更新律师-案件对应表
        CaseLawyerEntity caseLawyerEntity = new CaseLawyerEntity();
        caseLawyerEntity.setLawyerId(assignLawyerDTO.getLawyerId());
        caseLawyerEntity.setCaseNo(assignLawyerDTO.getCaseNo());
        caseLawyerEntity.setAssignTime(new Date());
        caseLawyerMapper.insert(caseLawyerEntity);

    }
}